package cn.com.dhc.danlu.business.favorite.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.MainMenuActivity;
import cn.com.dhc.danlu.business.favorite.FavoriteListDelegate;
import cn.com.dhc.danlu.business.favorite.IFavoriteListDataCallback;
import cn.com.dhc.danlu.business.favorite.adapter.FavoriteListAdapter;
import cn.com.dhc.danlu.util.ToastUtil;
import cn.com.dhc.danlu.widget.DLToolbar;
import cn.com.dhc.danlu.widget.DanluRecyclerView;
import cn.com.dhc.danlu.widget.dialog.ViewDialog;

/**
 * author: 李一方(<a href="mailto:nieqigang@danlu.com">nieqigang@danlu.com</a>)<br/>
 * author: 李一方(<a href="mailto:liyifang@danlu.com">liyifang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017.3.28 00:00<br/>
 *
 * <p>
 *
 * </p>
 */
public class FavoriteActivity extends AppCompatActivity implements IFavoriteListDataCallback, View.OnClickListener {
    private View emptyView;
    private Button deleteFavorite;
    private ImageView emptyViewImage;
    private TextView emptyViewText;
    private DanluRecyclerView favoriteGoodsList;
    private DLToolbar toolbar;
    private View errorRetry;

    private FavoriteListAdapter favoriteAdapter;
    private FavoriteListDelegate delegate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favorite);

        initView();
        setView();
        initApis();
    }

    @Override
    protected void onStart() {
        super.onStart();

        favoriteGoodsList.refresh();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.empty_view_image:// 跳转商品分类画面
                if (!getString(R.string.myself_collect_no_goods).equals(emptyViewText.getText())) {
                    return;
                }

                Intent intent = new Intent(FavoriteActivity.this, MainMenuActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("switchCategory", true);
                startActivity(intent);
                break;

            case R.id.delete_favorite:
                if (favoriteAdapter.isCurrentInEditMode() &&
                        favoriteAdapter.isAtLeastOneChecked()) {
                    showDeleteFavoriteDialog();
                }
                break;

            case R.id.btn_error_retry:
                favoriteGoodsList.refresh();
                break;

            default:
                break;
        }
    }

    @Override
    public void beforeRefresh() {
        //刷新之前，若当前视图无数据，隐藏编辑按钮
        if (favoriteAdapter.getItemCount() <= 0) {
            toolbar.setRightTxt("");
        }
    }

    @Override
    public void onSuccess(@NonNull List datas, boolean isRefresh) {
        if (!datas.isEmpty()) {
            toolbar.setRightTxt(getString(R.string.btn_edit));
            if (isRefresh) {
                favoriteAdapter.refreshAllData(datas);
            } else {
                favoriteAdapter.appendData(datas);
            }
        } else {
            toolbar.setRightTxt("");
            if (isRefresh) {
                //解决Adapter中只有一条数据时，将此条取消收藏后，不显示EmptyView的问题
                favoriteAdapter.refreshAllData(new ArrayList());
                setEmptyView(R.drawable.img_collect_add, R.string.myself_collect_no_goods);
            } else {
                //解决下拉刷新，编辑按钮不可见
                toolbar.setRightTxt(getString(R.string.btn_edit));
                favoriteGoodsList.setNoMore(true);
                ToastUtil.getInstance().show(R.string.no_more_data_string);
            }
        }
        favoriteGoodsList.refreshComplete();
    }

    @Override
    public void onError(@NonNull String errorInfo, boolean isRefresh) {
        setEmptyView(R.drawable.dl_common_error, R.string.network_exception);
        favoriteGoodsList.refreshComplete();
    }

    private void initView() {
        toolbar = (DLToolbar) findViewById(R.id.toolbar);
        emptyView = findViewById(R.id.empty_view);
        deleteFavorite = (Button) findViewById(R.id.delete_favorite);
        emptyViewImage = (ImageView) findViewById(R.id.empty_view_image);
        emptyViewText = (TextView) findViewById(R.id.empty_view_text);
        favoriteGoodsList = (DanluRecyclerView) findViewById(R.id.favorite_list);
        errorRetry = findViewById(R.id.btn_error_retry);
    }

    private void setView() {
        emptyViewImage.setOnClickListener(this);
        deleteFavorite.setOnClickListener(this);
        errorRetry.setOnClickListener(this);
        toolbar.setRightClickListenter(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (favoriteAdapter.isCurrentInEditMode() && favoriteAdapter.isAtLeastOneChecked()) {
                    showDeleteFavoriteDialog();
                } else {
                    updateCurrentMode(!favoriteAdapter.isCurrentInEditMode());
                }
            }
        });

        favoriteAdapter = new FavoriteListAdapter(FavoriteActivity.this);
        favoriteAdapter.refreshAllData(new ArrayList());

        favoriteGoodsList.setAdapter(favoriteAdapter);
    }

    private void initApis() {
        delegate = new FavoriteListDelegate();
        delegate.attachHostView(this);
        delegate.setRefreshViewAttr(favoriteGoodsList);
        favoriteGoodsList.setRefreshView(delegate);
    }

    /**
     * 编辑模式不支持上、下拉刷新
     */
    private void updateCurrentMode(boolean isEditMode) {
        if (isEditMode) {
            favoriteGoodsList.setPullRefreshEnabled(false);
            favoriteGoodsList.setLoadingMoreEnabled(false);
            toolbar.setRightTxt(getString(R.string.btn_finish));
            favoriteAdapter.setEditMode(true);
            deleteFavorite.setVisibility(View.VISIBLE);
        } else {
            favoriteGoodsList.setPullRefreshEnabled(true);
            favoriteGoodsList.setLoadingMoreEnabled(true);
            toolbar.setRightTxt(getString(R.string.btn_edit));
            favoriteAdapter.setEditMode(false);
            deleteFavorite.setVisibility(View.GONE);

            //处理删除完成后无数据的情况
            if (favoriteAdapter.getItemCount() <= 0) {
                setEmptyView(R.drawable.img_collect_add, R.string.myself_collect_no_goods);
                toolbar.setRightTxt("");
            } else {
                toolbar.setRightTxt(getString(R.string.btn_edit));
            }
        }
    }

    private void setEmptyView(int imageId, int textId) {
        if (R.string.myself_collect_no_goods != textId) {
            errorRetry.setVisibility(View.VISIBLE);
        } else {
            errorRetry.setVisibility(View.GONE);
        }
        emptyViewImage.setImageResource(imageId);
        emptyViewText.setText(textId);
        favoriteGoodsList.setEmptyView(emptyView);
    }

    private void showDeleteFavoriteDialog() {
        new ViewDialog.Builder(this).setMessage(
                getString(R.string.myself_collect_del_msg1))
                .setPositiveButton(getString(R.string.btn_delete),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                favoriteAdapter.deleteSelectedItem();
                            }
                        })
                .setNegativeButton(getString(R.string.btn_cancel),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                .create().show();
    }
}

